﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DesktopLauncher
{
    public static class TextManager
    {
        /// <summary>
        /// Maže diakritiku, nějaký komba to replacuje blbě, ale normální české znaky pohodička. Používá se při odesílání smsky.
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string RemoveDiacritics(this string inputString)
        {
            //!\\ Warning 'œ' will be replaced with a 'o' not an 'oe'
            String normalizedString = inputString.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < normalizedString.Length; i++)
            {
                Char c = normalizedString[i];
                if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.NonSpacingMark)
                    stringBuilder.Append(c);
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// Smaže neplatné znaky - používá se pro zápis do souboru, aby neobsahoval neplatné znaky
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string RemoveInvalidCharacters(this string str)
        {
            Regex rgx = new Regex("[^a-zA-Z0-9]");
            return rgx.Replace(str, "").RemoveDiacritics();
        }

        /// <summary>
        /// Will return unique alfanumeric string in specified length
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Uniqid(int length = 3)
        {
            if (length > 32)
                length = 32;
            return System.Guid.NewGuid().ToString().Replace("-", "").Substring(0, length);
        }
    }
}
